<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 后台退款单控制器
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/bill/billRefund.class.php');
quanli_require('module/base/bill/billSearch.class.php');

class pa_billRefund {

    public function execute() {

        if(isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0 ) {

            switch ($_REQUEST['operFlg']) {
                case 1:			//订单退款查询
                    $this->searchBillRefund();
                    break;
                case 2:			//添加退款单  
                	$this->billRefundAdd();
                	break;
                case 100:			//订单退款详情
                    $this->billRefundDetail();
                    break;

            }
        }
    }
    
    
    
    private function searchBillRefund(){

        global $G_PGLOB;

        //获取支付方式
        $result_payType = $G_PGLOB->DBCA->stmt_query("SELECT payTypeID,payTypeName FROM sys_paytype WHERE type = 2 AND useFlg = 1");
        $G_PGLOB->smarty->assign("payType",$result_payType);
        //$G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(1005));//退款单查询权限
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(1014));//退款单添加权限
        $G_PGLOB->smarty->assign('posted_permission', $G_PGLOB->has_permission(1017));//退款单过账
    	$G_PGLOB->smarty->display('bill/billRefundSearch.tpl');
    }

    
    
    
    private function billRefundAdd(){

        global $G_PGLOB;

        //获取支付方式
        $result_payType = $G_PGLOB->DBCA->stmt_query("SELECT payTypeID,payTypeName FROM sys_paytype WHERE  type = 2  AND useFlg = 1");
        $G_PGLOB->smarty->assign("payType",$result_payType);

    	$G_PGLOB->smarty->display('bill/billRefundAdd.tpl');
    }


    /**
     * 退款单详情页
     */
    private function billRefundDetail(){

        global $G_PGLOB;

        $billRefund = new billRefund($G_PGLOB->DBCA);

        if(!isset($_REQUEST['refund_no']) || !is_numeric($_REQUEST['refund_no']) ){
            $G_PGLOB->smarty->assign("error_msg","参数错误");
            $G_PGLOB->smarty->display('bill/billRefundDetail.tpl');
            die();
        }

        $arg_array = array(
            "refund_no" => $_REQUEST['refund_no']
        );
        $result_refund = $billRefund->searchBillRefund($arg_array);

        //查询商品信息
        if(!empty($result_refund[0]['return_no'])){         //有退货单先查退货商品
            $arg_array = array("return_no" => $result_refund[0]['return_no']);
            $result_goods = $billRefund->searchBillReturnGoods($arg_array);
            $G_PGLOB->smarty->assign("goods_array",$result_goods);
        }elseif(!empty($result_refund[0]['bill_no'])){
            $arg_array = array("bill_no" => $result_refund[0]['bill_no']);
            //实例化订单类
            $billSearch = new billSearch($G_PGLOB->DBCA);
            $result_goods = $billSearch->searchBillGoods($arg_array);
        }

        //查询该订单已过帐的退款信息
        $sql ="SELECT * FROM bill_refund WHERE bill_code = ? AND refund_no != ? AND refund_status = 4";
        $result_refunded = $G_PGLOB->DBCA->stmt_query($sql, 'ss', $result_refund[0]['bill_code'],$_REQUEST['refund_no']);

        //保存已过帐退款信息
        $refunded_amount = array();
        if($result_refunded){
            foreach($result_refunded as $key => $refunded){
                $refunded_amount[$key]['refund_no'] = $refunded->refund_no;
                $refunded_amount[$key]['refund_amount'] = $refunded->refund_amount;
            }
        }

        /*
         * add cdd start
         */
        //查询当前退款单的操作日志
        $refundLogSql='select * from log_detail where  logItem=? order by createTime desc';
        $refundLog=$G_PGLOB->DBCA->stmt_query($refundLogSql,'i',$_REQUEST['refund_no']);

        //查询此笔订单的 商品售价，实收运费，订单配件金额,代付订单 是否异地付款
        $bill_master_sql='select bill_no,bill_totalMoney,deliver_feePaid,bill_accessories_money,paying_agent_str,bill_other_address_pay from bill_master  where bill_code=?';
        $bill_info=$G_PGLOB->DBCA->stmt_query($bill_master_sql,'s',$result_refund[0]['bill_code']);

        $remote_service=0;//异地服务费
        $freight=$bill_info[0]->deliver_feePaid;//运费
        $accessories=$bill_info[0]->bill_accessories_money;//配件费

      //查询是否为异地付款单
      $bill_goods_sql='select product_code,goods_purchasePrice from bill_goods where bill_no=? and  product_code=?';
      $bill_gods_query=$G_PGLOB->DBCA->stmt_query($bill_goods_sql,'ss',$bill_info[0]->bill_no,'ptp05');
        //异地服务费
        if(!empty($bill_gods_query)){
            foreach($bill_gods_query as  $goods){
                $remote_service+=$goods->goods_purchasePrice;
            }
        }

        //如果存在代付订单
        if($bill_info[0]->paying_agent_str){
            $bill_order=$bill_info[0]->paying_agent_str;//代付订单字符串
            //获取代付订单bill_master信息
            $bill_other_sql="select bill_code,bill_no,deliver_feePaid,bill_accessories_money from  bill_master where bill_code in($bill_order)";
            $bill_other_info=$G_PGLOB->DBCA->stmt_query($bill_other_sql);
            //循环代付订单
            foreach($bill_other_info as  $other_info){

                //把配件费、运费累加起来
                $freight+=$other_info->deliver_feePaid;
                $accessories+=$other_info->bill_accessories_money;
                //循环获取代付订单的商品信息(商品号，商品名称，规格，销售价，实收价，积分价，积分，购买数量)
                $bill_goods_sql="SELECT bill_goods_no,bill_no,goods_goodsID,goods_name,goods_spec,goods_storePrice,goods_purchasePrice,goods_pointsPrice,goods_points,goods_amount FROM  bill_goods WHERE bill_no=?";
                $bill_goods_array=$G_PGLOB->DBCA->stmt_query($bill_goods_sql,'s',$other_info->bill_no);
                foreach($bill_goods_array  as $bill_goods){
                    $sql_fittings='select * from bill_goods_fittings where bill_goods_no =?';
                    $bill_goods_no=$G_PGLOB->DBCA->stmt_query($sql_fittings,'i',$bill_goods->bill_goods_no);
                    $buy_momney=0;
                    foreach($bill_goods_no as $bill_buy){
                        $buy_momney+=$bill_buy->buyMoney*$bill_buy->buyNum;
                    }
                    $other_goods_array[]=array(
                        'bill_code'=>$other_info->bill_code,
                        'goods_goodsID'=>$bill_goods->goods_goodsID,
                        'goods_name'=>$bill_goods->goods_name,
                        'goods_spec'=>$bill_goods->goods_spec,
                        'goods_storePrice'=>$bill_goods->goods_storePrice,
                        'goods_purchasePrice'=>$bill_goods->goods_purchasePrice,
                        'goods_pointsPrice'=>$bill_goods->goods_pointsPrice,
                        'goods_points'=>$bill_goods->goods_points,
                        'goods_amount'=>$bill_goods->goods_amount,
                        'buyMoney'=>$buy_momney
                    );
                }

            }
        }


        $G_PGLOB->smarty->assign("refundLog",$refundLog);//退款单日志
        $G_PGLOB->smarty->assign("freight",$freight);//运费
        $G_PGLOB->smarty->assign("accessories",$accessories);//配件费
        $G_PGLOB->smarty->assign("remote_service",$remote_service);//异地服务费
        $G_PGLOB->smarty->assign("other_goods_array",$other_goods_array);//代付订单商品信息
           /*
           * add cdd end
           */
        $G_PGLOB->smarty->assign("refund",$_REQUEST['refund']);
        $G_PGLOB->smarty->assign("goods_array",$result_goods);
        $G_PGLOB->smarty->assign("refund_info",$result_refund[0]);
        $G_PGLOB->smarty->assign("refunded_amount",$refunded_amount);
        $G_PGLOB->smarty->assign('check_permission', $G_PGLOB->has_permission(1015));//退款单审核权限
        $G_PGLOB->smarty->assign('cancle_permission', $G_PGLOB->has_permission(1016));//退款单取消权限
    	$G_PGLOB->smarty->display('bill/billRefundDetail.tpl');
    }
   


}
